<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $helpers = array('Pondcases','Ajax','Javascript');
	var $components = array('RequestHandler');

	function beforeRender() {
		$groupid = $this->Auth->user('group_id');
		$name = 'Comments';
		IF($groupid != 1){
			$result1 = mysql_query("SELECT id FROM applications WHERE name = '$name'");
			$appid = mysql_fetch_object($result1)->id;
			$result2 = mysql_query("SELECT count(*) as count FROM groupshasrights WHERE group_id = '$groupid' AND application_id = '$appid'");
			$count = mysql_fetch_object($result2)->count;
			IF($count == 0){
				$this->Session->setFlash(__('Access Denied', true));
				$this->redirect(array('controller' => 'pages' ,'action' => 'error'));	
			}
		}
	}
	
	function index() {
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid comment', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}


	function add() {
		if (!empty($this->data)) {
			$this->Comment->create();
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The comment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
			}
		}
		$pondcases = $this->Comment->Pondcase->find('list');
		$this->set(compact('pondcases'));
	}
	
	function add2() {
		$this->autoLayout = false;
        $id = $this->params['url']['caseid'];
		$div = $this->params['url']['div'];
		$stat = $this->params['url']['stat'];$this->layout = null;
        
		IF($stat == 1){
			$result = mysql_query("SELECT comment FROM comments WHERE pondcase_id = '$id'");
			$text = mysql_fetch_object($result)->comment;
			$char = "<br>"; 
        	$newchar = "\n";
        	$text = str_replace($char, $newchar, $text);
			$this->set(compact('text'));
		}		
		$this->set(compact('pondcases','id','div','stat'));
	}
	
	function save(){
		$this->layout = null;
        $this->autoLayout = false;
        $text = $this->data['Comment']['comment'];
        $pcid = $this->data['Comment']['pondcase_id'];
        $div = $this->data['Comment']['div'];    
        $stat = $this->data['Comment']['stat'];
        $char = "\n"; 
        $newchar = "<br>";
        $text = str_replace($char, $newchar, $text);
        $leer = "";
        IF($stat == 0){
        	mysql_query("INSERT INTO `mmst_helper`.`comments` (`id`, `comment`, `pondcase_id`) VALUES (NULL, '$text', '$pcid')");
        } 
        ELSEIF($text == $leer) {
        	mysql_query("DELETE FROM `mmst_helper`.`comments` WHERE pondcase_id = '$pcid' LIMIT 1");
        } 
        ELSE {
        	mysql_query("UPDATE `mmst_helper`.`comments` SET `comment` = '$text' WHERE `comments`.`pondcase_id` = '$pcid'");
        }  	
        $result = mysql_query("SELECT status_id AS status FROM pondcases WHERE id = '$pcid'");
        $status = mysql_fetch_object($result)->status;      
        $this->set(compact('pcid','div','status'));
	}
	

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid comment', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Comment->save($this->data)) {
				$this->Session->setFlash(__('The comment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
		$pondcases = $this->Comment->Pondcase->find('list');
		$this->set(compact('pondcases'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for comment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comment->delete($id)) {
			$this->Session->setFlash(__('Comment deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Comment was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>